#define _CRT_SECURE_NO_WARNINGS
#include "Sort.h"
#include"Stack.h"
void InsertSorttest()
{
	int a[] = { 9,7,8,10,12,3 };
	int len = sizeof(a) / sizeof(int);
	printarr(a, len);
	int begin = clock();
	InsertSort(a, len);
	int end = clock();
	printarr(a, len);
	printf("InsertSort=%d\n", end - begin);
}
void ShellSorttest()
{
	int a[] = { 9,7,8,10,12,3,235,632,63,63,74567,567,235,252345,346,634,432,4,32,443,636,346};
	int len = sizeof(a) / sizeof(int);
	printarr(a, len);
	int begin = clock();
	ShellSort(a, len);
	int end = clock();
	printarr(a, len);
	printf("ShellSort=%d\n", end - begin);
}
void TestOP()
{
	srand((unsigned)time(NULL));
	const int N = 1000000;
	int* a1 = (int*)malloc(sizeof(int) * N);
	int* a2 = (int*)malloc(sizeof(int) * N);
	int* a3 = (int*)malloc(sizeof(int) * N);
	int* a4 = (int*)malloc(sizeof(int) * N);
	int* a5 = (int*)malloc(sizeof(int) * N);
	int* a6 = (int*)malloc(sizeof(int) * N);
	int* a7 = (int*)malloc(sizeof(int) * N);
	int* a8 = (int*)malloc(sizeof(int) * N);
	int* a9 = (int*)malloc(sizeof(int) * N);
	
	for (int i = 0; i < N; ++i)
	{
		a1[i] = rand() + i;
		a2[i] = a1[i];
		a3[i] = a1[i];
		a4[i] = a1[i];
		a5[i] = a1[i];
		a6[i] = a1[i];
		a7[i] = a1[i];
		a8[i] = a1[i];
		a9[i] = a1[i];
	}

	int begin1 = clock();
	//InsertSort(a1, N);
	int end1 = clock();
	//printarr(a1, N);

	int begin2 = clock();
	ShellSort(a2, N);
	int end2 = clock();
	//printarr(a2, N);

	int begin3 = clock();
	//SelectSort(a3, N);
	int end3 = clock();

	int begin4 = clock();
	HeapSort(a4, N);
	int end4 = clock();

	int begin5 = clock();
	QuickSort(a5, 0, N - 1);
	int end5 = clock();

	int begin6 = clock();
	MergeSort(a6, N);
	int end6 = clock();

	int begin7 = clock();
	CountSort(a7, N);
	int end7 = clock();
	
	int begin8 = clock();
	QuickSortNonR(a8, 0, N - 1);
	int end8 = clock();

	int begin9 = clock();
	MergeSortNonR(a9, N);
	int end9= clock();

	

	printf("InsertSort:%d\n", end1 - begin1);
	printf("ShellSort:%d\n", end2 - begin2);
	printf("SelectSort:%d\n", end3 - begin3);
	printf("HeapSort:%d\n", end4 - begin4);
	printf("QuickSort:%d\n", end5 - begin5);
	printf("MergeSort:%d\n", end6 - begin6);
	printf("CountSort:%d\n", end7 - begin7);
	printf("QuickSortNonR:%d\n", end8 - begin8);
	printf("MergeSortNonR:%d\n", end9 - begin9);
	
	
	free(a1);
	free(a2);
	free(a3);
	free(a4);
	free(a5);
	free(a6);
	free(a7);
	free(a8);
	free(a9);
}
void SelectSorttest()
{
	int a[] = { 9,7,8,10,12,3,234,41};
	int len = sizeof(a) / sizeof(int);
	printarr(a, len);
	int begin = clock();
	SelectSort(a, len);
	int end = clock();
	printarr(a, len);
	printf("SelectSort=%d\n", end - begin);
}
void HeapSorttest()
{
	int a[] = { 5,11,7,2,3,17};
	int len = sizeof(a) / sizeof(int);
	printarr(a, len);
	int begin = clock();
	HeapSort(a, len);
	int end = clock();
	printarr(a, len);
	printf("HeapSort=%d\n", end - begin);
}
void BubbleSorttest()
{
	int a[] = { 9,7,8,10,12,3,532,525,525 };
	int len = sizeof(a) / sizeof(int);
	printarr(a, len);
	int begin = clock();
	BubbleSort(a, len);
	int end = clock();
	printarr(a, len);
	printf("BubbleSorttest=%d\n", end - begin);
}
void PartSort2test()
{
	int a[] = { 9,7,8,10,12,3,532,525,525,235,32,41,52,5212,121,3,45,52,3,4,3 };
	int  len = sizeof(a) / sizeof(int);
	printarr(a, len);
	int begin = clock();
	PartSort2(a, 0,len-1);
	int end = clock();
	printarr(a, len);
	printf("PartSort2=%d\n", end - begin);
}
void PartSort1test()
{
	int a[] = { 9,7,8,10,12,3,532,525,525,235,32,41,52,5212,121,3,45,52,3,4,3 };
	int  len = sizeof(a) / sizeof(int);
	printarr(a, len);
	int begin = clock();
	PartSort1(a, 0, len - 1);
	int end = clock();
	printarr(a, len);
	printf("PartSort1=%d\n", end - begin);
}
void PartSort3test()
{
	int a[] = { 9,7,8,10,12,3,532,525,525,235,32,41,52,5212,121,3,45,52,3,4,3 };
	int  len = sizeof(a) / sizeof(int);
	printarr(a, len);
	int begin = clock();
	PartSort3(a, 0, len - 1);
	int end = clock();
	printarr(a, len);
	printf("PartSort3=%d\n", end - begin);
}
void QuickSorttest()
{
	int a[] = { 65,56,72,99,86,25,34,66 };
	int  len = sizeof(a) / sizeof(int);
	printarr(a, len);
	int begin = clock();
	QuickSort(a, 0, len - 1);
	int end = clock();
	printarr(a, len);
	printf("QuickSort=%d\n", end - begin);
}
void TestQuickSortNonR()
{
	int a[] = { 9,7,8,10,12121,3,45,52,4,24,235,4,9,8 };
	int  len = sizeof(a) / sizeof(int);
	printarr(a, len);
	int begin = clock();
	QuickSortNonR(a, 0, len - 1);
	int end = clock();
	printarr(a, len);
	printf("QuickSortNonR=%d\n", end - begin);
}
void MergeSorttest()
{
	int a[] = { 9,7,8,10,12121,3,45,52,4,24,235,4,9,8 };
	int  len = sizeof(a) / sizeof(int);
	printarr(a, len);
	int begin = clock();
	MergeSort(a, len);
	int end = clock();
	printarr(a, len);
	printf("MergeSort=%d\n", end - begin);
}
void MergeSortNonRtest()
{
	int a[] = { 9,7,8,10,12121,3,45,52,4,24,235,4,9,8 };
	int  len = sizeof(a) / sizeof(int);
	printarr(a, len);
	int begin = clock();
	MergeSortNonR(a, len);
	int end = clock();
	printarr(a, len);
	printf("MergeSortNonR=%d\n", end - begin);
}
void shellsort(int* a, int n)
{
	
	int gap = n;
	while (gap>1)
	{
		gap = gap / 3 + 1;
		
			for (int i = 0; i < n - gap; i++)
			{
				int end = i;
				int key = a[end + gap];
				while (end >= 0)
				{
					if (key < a[end])
					{
						a[end + gap] = a[end];
						end-=gap;
					}
					else
					{
						break;
					}
				}
				a[end + gap] = key;
			}

	}
}
void testCountSort()
{
	int a[] = { 9,7,8,10,12121,3,45,52,24,235 };
	int  len = sizeof(a) / sizeof(int);
	printarr(a, len);
	int begin = clock();
	CountSort(a, len);
	int end = clock();
	printarr(a, len);
	printf("CountSort=%d\n", end - begin);
}
int main()
{
	//InsertSorttest();
	//ShellSorttest();
	//TestOP();
	//SelectSorttest();
	//HeapSorttest();
	//BubbleSorttest();
	//PartSort2test();
	//PartSort1test();
	//PartSort3test();
	QuickSorttest();
	//TestQuickSortNonR();
	/*MergeSorttest();
	MergeSortNonRtest();*/
	/*int a[] = { 9,7,8,10,12121,3,45,52,4,24,235};
	int  len = sizeof(a) / sizeof(int);
	printarr(a, len);
	shellsort(a, len);
	printarr(a, len);*/
	//testCountSort();
	return 0;
}

